library(marginaleffects)

# Start log file for script -----------------------------------------------
TeachingDemos::txtStart("./log files/figureA17_blackwhite_prep.txt")


# Load Data ---------------------------------------------------------
# individual data
load("./data/impdat20.Race.WeekWeight.rdata")

# Equalizing weeks with Nationscape
impdat20.WeekWeight$week2 <- as.numeric(impdat20.WeekWeight$week) + 1
impdat20.WeekWeight$week2[which(impdat20.WeekWeight$week2 == 53)] <-  52
impdat20.WeekWeight$week2 <- as.character(impdat20.WeekWeight$week2)
impdat20.WeekWeight$week2[which(nchar(impdat20.WeekWeight$week2) == 1)] <- paste0("0", impdat20.WeekWeight$week2[which(nchar(impdat20.WeekWeight$week2) == 1)])
impdat20.WeekWeight <- fastDummies::dummy_cols(impdat20.WeekWeight, select_columns = "week2")

impdat20.WeekWeight$week2_post <- "Pre-GF"
for(i in 22:52){
  impdat20.WeekWeight$week2_post[which(impdat20.WeekWeight[, paste0("week2_", i)] == 1)] <- paste("Week", i)
}

# Race
impdat20.WeekWeight$race_black <- ifelse(impdat20.WeekWeight$race3 == "Black", "Black", "Not Black")
impdat20.WeekWeight$race_black[which(is.na(impdat20.WeekWeight$race3))] <- NA

# Models --------------------------------------------------------------
# full pre-post
m1_prepost_bw <- lm(D_biep.White_Good_all ~ post_gf*race_black + 
                      age_cat4 + edu_cat4 +  ideo_mod + ideo_con + sex +
                      census_region + broughtwebsite2,
                    data = impdat20.WeekWeight, weights = WeekWeights,
                    subset = race3 == "White" | race3 == "Black")
summary(m1_prepost_bw)

# Weekly after
m1_pre_weeklypost_bw <- lm(D_biep.White_Good_all ~ week2_post*race_black + 
                             age_cat4 + edu_cat4 +  ideo_mod + ideo_con + sex +
                             census_region + broughtwebsite2,
                           data = impdat20.WeekWeight, weights = WeekWeights,
                           subset = race3 == "White" | race3 == "Black")
summary(m1_pre_weeklypost_bw)



# Plot --------------------------------------------------------------------
# pre-post full
d <- plot_cme(m1_prepost_bw, "post_gf", "race_black", draw = F)
prepost <- data.frame(race = c("Whites", "Blacks"),
                      coef = d$estimate,
                      se = d$std.error)

# pre weekly post
dd <- plot_cme(m1_pre_weeklypost_bw, "week2_post", "race_black", draw = F)
dd_wht <- subset(dd, race_black == "Not Black")
dd_blk <- subset(dd, race_black == "Black")
weekly_outs <- data.frame(race = c(rep("Blacks", nrow(dd_blk)),
                                   rep("Whites", nrow(dd_wht))),
                          week = c(gsub(" - Pre-GF", "", dd_blk$contrast),
                                   gsub(" - Pre-GF", "", dd_wht$contrast)),
                          coef = c(dd_blk$estimate, dd_wht$estimate),
                          se = c(dd_blk$std.error, dd_wht$std.error))

# Combine to plot
prepost <- subset(prepost, select = c("race", "coef", "se"))
prepost$week <- "Post GF, May 26, 2020-Dec 31, 2020"

coef_out <- rbind(prepost, weekly_outs)

coef_out$race <- factor(coef_out$race,
                        levels = c("Whites", "Blacks"))
coef_out$var <- as.factor(coef_out$week)
coef_out$var <- factor(coef_out$var,
                       levels = rev(levels(coef_out$var)))
coef_out$indx <- c(1, 1, rep(0, nrow(weekly_outs)))
coef_out$indx <- ifelse(coef_out$indx == 1, "Pre-Post", "Weekly Indicators")

coef_out_bw <- coef_out

# End log file for script -------------------------------------------------
TeachingDemos::txtStop()